<?php
// 抽奖
class ScratchAction extends UserAction {
	//魔术方法
public function __construct(){
		parent::__construct();
		
	}
  public function index(){
      $this->data=$this->get_today_draw();
	    $this->display();
  }

  public function get_today_draw(){
        $uid=$this->uid;
        $todaytime=strtotime(date('Y-m-d',time()));
        $tmdaytime=strtotime(date('Y-m-d',strtotime("+1 day")));
        $where['member_id']=$uid;
        $where['draw_type']=15;
        $where['add_time']=array('between',array($todaytime,$tmdaytime));
        $data=M('draw_log')->where($where)->find();
        $data=$this->get_draw_msg($data);
        return $data;
    }
    //获取 奖池奖品get_rand
  public function draw_list(){
    $p=$_REQUEST['p'];
     $pagesize=10;
     $p=!empty($p)?$p:1;
     $start=($p-1)*$pagesize;
     $where['member_id']=$this->uid; 
     $list=M('draw_log')->where($where)//
           ->limit($start,$pagesize)//
           ->order('add_time desc')->select();
     if($list){
      $draw_address_model=M('draw_address');
       foreach ($list as $key => $value) {
         $value['add_time']=date("Y/m/d",$value['add_time']);
         $value['draw_address']=0;
         if($value['draw_type']==5){
          $value['draw_address']=$draw_address_model->where(array('draw_log_id'=>$value['id']))->count();
         }
         $list[$key]=$this->get_draw_msg($value);
       }
     }
     if(IS_AJAX){
      echo json_encode($list);die;
     }
      $this->now_address=M('g_user_draw_address')->where(array('user_id'=>$this->uid))->find();
      $this->count=M('draw_log')->where($where)->count();
      $this->list=$list;
      $this->display();
  }
   //设置收货地址
  public function draw_address(){
    if (IS_AJAX) {
      $address_id=$_REQUEST['address_id'];
      $return_data['status']=1;
      $return_url=U('wap/draw/draw_list');
      $return_data['return_url']=$return_url;
      if(empty($address_id)){
        echo json_encode($return_data);die;
      }
      $address=M('g_user_address')->where(array('address_id'=>$address_id,'user_id'=>$this->uid))->find();
      if(empty($address)){
        $return_data['status']=0;
         $return_data['error']="该地址不存在";
         echo json_encode($return_data);die;
      }
      $add_data['address_name']=$address['address_name'];
      $add_data['user_id']=$address['user_id'];
      $add_data['consignee']=$address['consignee'];
      $add_data['email']=$address['email'];
      $add_data['country']=$address['country'];
      $add_data['province']=$address['province'];
      $add_data['city']=$address['city'];
      $add_data['area']=$address['area'];
      $add_data['address']=$address['address'];
      $add_data['zipcode']=$address['zipcode'];
      $add_data['tel']=$address['tel'];
      $add_data['mobile']=$address['mobile'];
      $add_data['sign_building']=$address['sign_building'];
      $add_data['best_time']=$address['best_time'];
      $count=M('g_user_draw_address')->where(array('user_id'=>$this->uid))->find();
      if($count){
        $res=M('g_user_draw_address')->where(array('id'=>$count['id']))->save($add_data);
      }else{
        $res=M('g_user_draw_address')->add($add_data);
      }
      if($res!==false){
        echo json_encode($return_data);die;
      }else{
         $return_data['status']=0;
         $return_data['error']="操作失败";
         echo json_encode($return_data);die;
      }
     }
     $this->now_address=M('g_user_draw_address')->where(array('user_id'=>$this->uid))->find();
     $this->address=M('g_user_address')->where(array('user_id'=>$this->uid))->order('is_default desc')->select();
     
     $this->display();
  }
   //设置收货地址
  public function set_draw_address(){
    if (IS_AJAX) {
      $address_id=$_REQUEST['address_id'];
      $draw_log_id=$_REQUEST['draw_log_id'];
      $return_data['status']=1;
      $return_url=U('wap/draw/draw_list');
      $return_data['return_url']=$return_url;
      if(empty($address_id)){
        echo json_encode($return_data);die;
      }
      $draw_address_model=M('draw_address');
       $draw_address=$draw_address_model->where(array('member_id'=>$this->uid,'draw_log_id'=>$draw_log_id))->find();
     if($draw_address){//地址已设置
        echo json_encode($return_data);die;
     }
      $address=M('g_user_address')->where(array('address_id'=>$address_id,'user_id'=>$this->uid))->find();
      if(empty($address)){
        $return_data['status']=0;
         $return_data['error']="该地址不存在";
         echo json_encode($return_data);die;
      }
      $g_user_draw_address=M('g_user_draw_address')->where(array('user_id'=>$this->uid))->count();
      if(empty($g_user_draw_address)){///添加默认收货地址
        $add_data=array();
         $add_data['address_name']=$address['address_name'];
        $add_data['user_id']=$address['user_id'];
        $add_data['consignee']=$address['consignee'];
        $add_data['email']=$address['email'];
        $add_data['country']=$address['country'];
        $add_data['province']=$address['province'];
        $add_data['city']=$address['city'];
        $add_data['area']=$address['area'];
        $add_data['address']=$address['address'];
        $add_data['zipcode']=$address['zipcode'];
        $add_data['tel']=$address['tel'];
        $add_data['mobile']=$address['mobile'];
        $add_data['sign_building']=$address['sign_building'];
        $add_data['best_time']=$address['best_time'];
         M('g_user_draw_address')->add($add_data);
      }
      $add_data=array();
        $add_data['draw_log_id']=$draw_log_id;
      $add_data['address_name']=$address['address_name'];
      $add_data['member_id']=$address['user_id'];
      $add_data['consignee']=$address['consignee'];
      $add_data['email']=$address['email'];
      $add_data['address']=$address['address'];
      $add_data['zipcode']=$address['zipcode'];
      $add_data['tel']=$address['tel'];
      $add_data['mobile']=$address['mobile'];
      $add_data['sign_building']=$address['sign_building'];
      $add_data['best_time']=$address['best_time'];

      $res=$draw_address_model->add($add_data);
      
      if($res!==false){
        echo json_encode($return_data);die;
      }else{
         $return_data['status']=0;
         $return_data['error']="操作失败";
         echo json_encode($return_data);die;
      }
     }
     $now_address=M('draw_address')->where(array('user_id'=>$this->uid,'draw_log_id'=>$_GET['draw_log_id']))->find();
     $this->now_address=$now_address;
     if(empty($now_address)){
      $this->address=M('g_user_address')->where(array('user_id'=>$this->uid))->order('is_default desc')->select();
     }
     $this->display();
  }
   
    //获取 奖池奖品get_rand
  public function get_draw_list(){
     $where['draw_status']=1;
     $where['is_shenghe']=1;
     $list=M('draw_progressive')->where($where)->select();
     $return_data=array();
     if($list){
       foreach ($list as $key => $value) {
         $return_data[$value['id']]=$value['draw_chances_value'];
       }
     }
     return $return_data;
  }
   //验证抽奖权限
  public function check_draw(){
      $uid=$this->uid;
      $todaytime=strtotime(date('Y-m-d',time()));
      $tmdaytime=strtotime(date('Y-m-d',strtotime("+1 day")));
      $where['member_id']=$uid;
      $where['draw_id']=15;
      $where['add_time']=array('between',array($todaytime,$tmdaytime));
      $data=M('draw_log')->where($where)->count();
     return $data;
  }
 
  //查询并设置抽奖记录
  public function set_draw_msg($draw_data=null)
  {
	  if (empty($draw_data) ) {
		  return array();
	  }
	  $uid = $this->uid;
	  $log_data = array();
	  $log_data['member_id'] = $uid;
	  $log_data['draw_id'] = $draw_data['id'];
	  $log_data['draw_type'] = $draw_data['draw_type'];
	  $log_data['draw_value'] = $draw_data['draw_value'] ;
	  $log_data['add_time'] = $draw_data['add_time']=time();
      $draw_data['statuse']=1;
      $draw_data['is_shenghe']=1;
	  $res = M('draw_log')->add($log_data);//log_status
	  $draw_data['draw_type_name'] = '现金';
	  $balance = $draw_data['draw_value'];
	  if ($balance > 0) {
		      $log = array();
		      $log['des'] = "每日抽奖中奖" . $balance;
              $member_integral=M('member_integral');
              $member_detail_model=M('member_detail');
              $field="member_id,points,balance";
              $where['member_id']=$uid;
              $memberinfo=$member_detail_model->Where($where)->field($field)->find();
              if(empty($memberinfo)){
                  return false;
              }
              $m_s_data['balance']=$memberinfo['balance']+$balance ;
              $log['status']=1;
              $log['type']=15;
              $log['add_time']=time();
              $log['member_id']=$uid;
              $log['integral']=$balance;
              $member_detail_model->startTrans();//开起事务
              $member_integral->startTrans();//开起事务

              $m_res=$member_detail_model->where($where)->save($m_s_data);
              $c_res=$member_integral->add($log);
              if($c_res!==false && $m_res!==false){
                  $member_integral->commit();//提交事务
                  $member_detail_model->commit();//提交事务
              }else{
                  $member_detail_model->rollback();//回滚事务
                  $member_integral->rollback();//回滚事务
              }
		  if ($m_res) {
			  M('draw_log')->where(array('id' => $res))->save(array('log_status' => 5));//log_status
		  }
	  }
	  return $draw_data;
  }
    //抽奖
  public function get_draw(){
     $return_data['status']=0;
     //抽奖开关
     $switch_draw=M('sys_param')->where(array('param_code'=>'switch_draw'))->getField('param_value'); 
     if(empty($switch_draw)){
       $return_data['error']="抽奖未开启";
       echo json_encode($return_data);die;
     }
     $check_draw=$this->check_draw();
     if($check_draw){
       $return_data['error']="每天只能抽一次奖";
       echo json_encode($return_data);die;
     }
     //奖品池
	  $draw=D('Jackpot')->today_bonus();
      $draw_data['id']=15;
	  $draw_data['draw_type']='15';
	  $draw_data['draw_value']=mb_number($draw);
      $draw_data['is_shenghe']=1;
     $draw_msg=$this->set_draw_msg($draw_data);
	 if(empty($draw_msg)||$draw_msg['draw_value']==0){
       $return_data['error']="未中奖";
       echo json_encode($return_data);die;
     }
     $draw_msg=$this->get_draw_msg($draw_msg);
     $return_data['status']=1;
     $return_data['draw']=$draw_msg;
     echo json_encode($return_data);die;
  }
	
}